﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using PhoneApp1.Model;
using PhoneApp1.ContextViewModel;
namespace PhoneApp1.ContextViewModel
{
    public class ExpenseViewModel : INotifyPropertyChanged
    {
        //Linq to SQL data context for local database
        //private AllDataContext m_DB;

        // class constructor, create the data context object
        public ExpenseViewModel()
        {
            // m_DB = new AllDataContext(m_DBConnetionString);
        }

        // All Expense Items
        private ObservableCollection<Expense> _allExpenseItems;
        public ObservableCollection<Expense> AllExpenseItems
        {
            get { return _allExpenseItems; }
            set
            {
                _allExpenseItems = value;
                NotifyPropertyChanged("AllExpenseItems");
            }
        }

        //write changes in the data context to database
        public void SaveChangesToDB()
        {
            App.DataViewContent.m_DB.SubmitChanges();
        }

        //Query  database and load the collection and list
        public void LoadCollectionsFromDatabase()
        {
            // Specify the query for all Expense items in database
            var expenseItemsInDB = from Expense expense in App.DataViewContent.m_DB.ExpenseItems
                                  select expense;
            AllExpenseItems = new ObservableCollection<Expense>(expenseItemsInDB);
        }

        // add a new item to the data context and the collection
        public void AddExpenseItems(Expense newItem)
        {
            // Add new Items to data context
            App.DataViewContent.m_DB.ExpenseItems.InsertOnSubmit(newItem);

            // Save changes to database
            App.DataViewContent.m_DB.SubmitChanges();

            // add to all Observable collections
            AllExpenseItems.Add(newItem);
        }

        // remover a item from data context and the conllection
        public void DeleteExpenseItems(Expense delItem)
        {

            // remover item from Observable conllection
            AllExpenseItems.Remove(delItem);

            // remover item from data context
            App.DataViewContent.m_DB.ExpenseItems.DeleteOnSubmit(delItem);

            // Save change to database
            App.DataViewContent.m_DB.SubmitChanges();

        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
